﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Diagnostics;

namespace Axial.Tests
{
	[JSSafe]
	public class SafeClass
	{
		public SafeClass()
		{
			Property = 0;
			StaticField = 3.0;
		}
		public SafeClass(int i)
		{
			Property = i;
		}
		public SafeClass(string s)
		{
			Field = s;
		}

		public static double StaticField { get; set; }
		public string Field = "Initialized";
		public int Property { get; set; }

		public void VoidMethod()
		{
			Property = 2;
		}

		public string FancyMethod(string arg0)
		{
			return arg0 + "done";
		}

		public static double StaticMethod()
		{
			return StaticField;
		}
	}

	[TestFixture]
	public class ClassTests : TestBase
	{
		[Test]
		public void SafeClassTest()
		{
			string check = 
@"function SafeClass()
{
	this.Field = null;
	var Propertyk__BackingField = 0;
	this.constructor.StaticFieldk__BackingField = 0;
	this.get_Property = function()
	{
		return this.Propertyk__BackingField;
	}
	this.set_Property = function(arg0)
	{
		this.Propertyk__BackingField = arg0;
	}
	this.VoidMethod = function()
	{
		this.set_Property(2);
	}
	this.FancyMethod = function(arg0)
	{
		return (arg0) + ('done');
	}
	this.get_StaticField = function()
	{
		return this.constructor.StaticFieldk__BackingField;
	}
	this.set_StaticField = function(arg0)
	{
		this.constructor.StaticFieldk__BackingField = arg0;
	}
	this.StaticMethod = function()
	{
		return this.get_StaticField();
	}
	
	if (arguments.length == 0)
	{
		this.Field = 'Initialized';
		this.set_Property(0);
		this.set_StaticField(3);
	}
	if (arguments.length == 1 && typeof arguments[0] == ""number"")
	{
		this.Field = 'Initialized';
		this.set_Property(arguments[0]);
	}
	if (arguments.length == 1 && typeof arguments[0] == ""string"")
	{
		this.Field = 'Initialized';
		this.Field = arguments[0];
	}
}";

			string code = JSWebTranslator.DissembleClass(typeof(SafeClass));
			code = code.Trim(); // remove ending newline
			Trace.WriteLine("SafeClass class");
			Trace.WriteLine("------------------------------------");
			Trace.WriteLine(code);
			Trace.WriteLine("");
			Assert.IsTrue(code.EndsWith(check.Trim()));
		}

		[Test]
		public void ClassUsageTest()
		{
			VerifyMethodOutput("ClassUsage",
@"a = new SafeClass();
b = new SafeClass(3);
c = new SafeClass('a');
a.FancyMethod('arg0');
b.Field = 'Value';
c.set_Property(3);
a.VoidMethod();");
		}

		public void ClassUsage()
		{
			var a = new SafeClass();
			var b = new SafeClass(3);
			var c = new SafeClass("a");

			a.FancyMethod("arg0");
			b.Field = "Value";
			c.Property = 3;
			a.VoidMethod();
		}
	}
}
